package com.lemon.boot.common.properties;

import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author 李猛
 * @datetime 2024/2/24 10:51
 * @description 类对象
 */
@Setter
@Getter
@Component
@ConfigurationProperties(prefix = "jwt")
public class JwtConfigProperties {
    //请求头
    private String header;
    //密钥
    private String secret;
    //超时时间
    private Integer expireTime;
    //放行的接口
    private String[] passUri;
    //放行的静态资源
    private String[] passStaticUri;
    //允许跨域的方法
    private String[] allowMethod;

    /**
     * 获取允许跨域的方法
     *
     * @return
     */
    public List<String> getAllowMethodByList() {
        if (allowMethod == null || allowMethod.length == 0) {
            return new ArrayList<>(0);
        }
        return Arrays.asList(allowMethod);
    }

    /**
     * 获取放行的接口
     *
     * @return
     */
    public List<String> getPassUriByList() {
        if (passUri == null || passUri.length == 0) {
            return new ArrayList<>(0);
        }
        return Arrays.asList(passUri);
    }

    /**
     * 获取放行的静态资源
     *
     * @return
     */
    public List<String> getPassStaticUriByList() {
        if (passStaticUri == null || passStaticUri.length == 0) {
            return new ArrayList<>(0);
        }
        return Arrays.asList(passStaticUri);
    }

    /**
     * 获取所有放行的接口
     *
     * @return
     */
    public List<String> getAllPassUriByList() {
        return Stream.concat(getPassUriByList().stream(), getPassStaticUriByList().stream())
                .collect(Collectors.toList());
    }

    /**
     * 获取所有放行的接口
     *
     * @return
     */
    public String[] getAllPassUriByArray() {
        return getAllPassUriByList().toArray(new String[0]);
    }
}
